package com.vogue.supplymall.payment.tools;

import java.io.UnsupportedEncodingException;

public class Base16 {
	
	public static String hex2Str(String theHex) {  
        char[] chars = theHex.toCharArray();  
        int len = chars.length / 2;  
        byte[] theByte = new byte[len];  
  
        for (int i = 0; i < len; i++) {  
            theByte[i] = Integer.decode("0X" + chars[i*2] + chars[i*2+1]).byteValue();  
        }  
  
        return new String(theByte);  
    }  
  
    public static String str2Hex(String theStr) throws UnsupportedEncodingException {  
        int tmp;  
        String tmpStr;  
        byte[] bytes = theStr.getBytes("UTF-8");  
        StringBuffer result = new StringBuffer(bytes.length * 2);  
  
        for (int i = 0; i < bytes.length; i++) {  
            tmp = bytes[i];  
            if (tmp < 0) {  
                tmp += 256;  
            }  
  
            tmpStr = Integer.toHexString(tmp);  
            if (tmpStr.length() == 1) {  
                result.append('0');  
            }  
  
            result.append(tmpStr);  
        }  
  
        return result.toString();  
    }  
  
    public static void main(String[] argv) {  
        String a = "你好a";  
        String b = null;
		try {
			b = str2Hex(a);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
        String c = hex2Str(b);  
        System.out.println(a);  
        System.out.println(b);  
        System.out.println(c);  
    }  
}
